import redis

from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from goods.models import SKU


def test_cors(request):
    return HttpResponse('ok')


r = redis.Redis(host='localhost',port=6379,db=0)

def test_stock(request):
    """测试redis分布式锁"""
    with r.lock('dashopt:stock',blocking_timeout=5) as lock: #blocking_timeout=5防止死锁
        sku = SKU.objects.get(id=3)
        sku.stock -= 1
        sku.save()

    return JsonResponse({'code':200})

